package com.swh.controller;

import com.swh.entity.Article;
import com.swh.entity.PageEntity;
import com.swh.servic.ArticleService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/24 19:19
 * @Version: 1.0
 */

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {
    private ArticleService articleService = new ArticleService();

    //查询发布用户+分页
    public void findPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> param = getParam(request);
        // 当前页
        Integer currPage = Integer.parseInt(param.get("currPage"));
        // 搜索条件
        String title = param.get("title");

        PageEntity<Article> pageData = articleService.findPage(currPage, title);

        request.setAttribute("pageData", pageData);

        //用于回显搜索条件
        request.setAttribute("title", title);
        request.getRequestDispatcher("/html/article.jsp").forward(request, response);


    }

    //发布文章（添加）
    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");

        Map<String, String> param = getParam(request);

        Article article = new Article();

        BeanUtils.populate(article, param);

        // 发布人信息
        article.setUserId(login.getId());
        article.setPublishRealName(login.getRealName());

        articleService.add(article);

        // 添加成功后重新转发到findPage填充数据
//        response.sendRedirect("/article/findPage?currPage=1");

        response.sendRedirect("/article/findPage?currPage=1");

    }

    //文章详情
    public void articleDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> param = getParam(request);

        Long id = Long.parseLong(param.get("id"));

        // 浏览量+1
        articleService.updateBrowserCount(id);
        //收藏次数
        Integer favoriteCount = articleService.countFavoriteByArticleId(id);
        //我是否收藏过的文章
        Boolean isFavorite = articleService.isFavorite(login.getId(), id);
        // 文章详情
        Article article = articleService.findById(id);

        // 我关注的人里面也收藏过这篇文章的人
        List<Map<String, Object>> favoriteList = articleService.findUserFavorite(login.getId(), id);

        request.setAttribute("article", article);
        request.setAttribute("favoriteCount", favoriteCount);
        request.setAttribute("isFavorite", isFavorite);
        request.setAttribute("favoriteList", favoriteList);

        request.getRequestDispatcher("/html/article_detail.jsp").forward(request, response);


    }

    //收藏/取消收藏
    public void favorite(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> param = getParam(request);

        long id = Long.parseLong(param.get("id"));

        // true:用户已经收藏过了  false:用户还没有收藏过
        Boolean favorite = articleService.isFavorite(login.getId(), id);

        if (favorite) {
            //已经收藏了；取消收藏
            articleService.delFunFavorite(login.getId(), id);
            writeObjToString(response, "0");
        } else {
            //还没有收藏；收藏
            articleService.addFavorite(login.getId(), id);
            writeObjToString(response, "1");
        }
    }

    //我的收藏+分页
    public void favoritePage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> param = getParam(request);

        Integer currPage = Integer.parseInt(param.get("currPage"));

        String title = param.get("title");

        PageEntity<Article> pageData = articleService.favoritePage(currPage, title, login.getId());

        request.setAttribute("pageData", pageData);
        // 用于回显搜索条件
        request.setAttribute("title", title);
        request.getRequestDispatcher("/html/article_collect.jsp").forward(request, response);
    }

}
